package com.thread.State;

public class TestState{

    public static void main(String[] args) {
        Thread thread = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("xxx");
        });


        //观察线程状态：
        Thread.State state = thread.getState();
        System.out.println(state);//new
        //启动观察
        thread.start();
        state =thread.getState();//更新线程状态
        System.out.println(state);//run
        //启动后观察
        while (state!=Thread.State.TERMINATED){//主要线程不中止，就一直监听状态
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            state =thread.getState();//更新线程状态
            System.out.println(state);//输出得到的线程状态。注意：while循环内部要有赋值语句，否则不能改变变量值

        }
    }
}
